package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.SysteminfoListAdapter;
import com.hxkcloud.nurse.beans.ArticleBeanKind;
import com.hxkcloud.nurse.beans.SystemMsgBean;
import com.hxkcloud.nurse.beans.SystemMsgWholeBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/18.
 * 消息，系统消息
 */
public class SystemInfoActivity extends BaseActivity implements View.OnClickListener, PullToRefreshLayout.OnRefreshListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private LinearLayout ll_empty;
    private TextView tv_empty;

    private SystemMsgWholeBean wholeBean;
    private int totalCount = 0;
    private List<SystemMsgBean> msgList;
    private SysteminfoListAdapter adapter;

    private int pageIndex = 1;
    private int pageSize = 15;
    private int loadmoreType = 0;//0第一次获取1刷新2更多

    private ArticleBeanKind articleBeanKind;
    private String articleInfoId = "";
    private String kindName = "";
    // [170514] 当前的评论Id，用于跳转
    private String nowCommentId = "";
//    private String type = "";
    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETSYSTEMMSG_SUCCESS:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    msgList.clear();
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (msg.obj != null) {
                    wholeBean = (SystemMsgWholeBean) msg.obj;
                    totalCount = wholeBean.getTotalCount();
                    if (wholeBean.getList() != null && wholeBean.getList().size() > 0) {
                        msgList.addAll(wholeBean.getList());
                        adapter.setData(msgList);
                    }
                } else {
                    toastMsg(R.string.datagetfail);
                }

                if (msgList == null || msgList.size() == 0) {
                    ll_empty.setVisibility(View.VISIBLE);
                }
                break;
            case MessageCode.NETWORKCODE_GETSYSTEMMSG_FAILURE:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datagetfail);
                }

                if (msgList == null || msgList.size() == 0) {
                    ll_empty.setVisibility(View.VISIBLE);
                }
                break;
            case MessageCode.NETWORKCODE_CONNECT_ERROR:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
                break;
            case MessageCode.NETWORKCODE_GETSYSTEMMSGKIND_SUCCESS:
                if (msg.obj!=null){
                    articleBeanKind = (ArticleBeanKind) msg.obj;
                    kindName = articleBeanKind.getKindName();
                    //type = articleBeanKind.getType();
                    Intent systemInfo = new Intent(mContext,ArticleDetailActivity.class);
                    systemInfo.putExtra("articleId",articleInfoId);
                    systemInfo.putExtra("kindName",kindName);
                    systemInfo.putExtra("commentId", nowCommentId);
                    //systemInfo.putExtra("type",type);
                    startActivity(systemInfo);
                }

                break;

            case MessageCode.NETWORKCODE_GETSYSTEMMSGKIND_FAILURE:

                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_systeminfo);
        msgList = new ArrayList<>();
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.systeminfo_text_title));
        iv_back.setOnClickListener(this);


        refreshLayout = (PullToRefreshLayout) findViewById(R.id.systeminfo_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.systeminfo_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
        adapter = new SysteminfoListAdapter(this, msgList);
        recycleView.setAdapter(adapter);
        adapter.setOnItemClickLitener(new SysteminfoListAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(int position) {
                if (!"0".equals(msgList.get(position).getArticleInfoId())){
                    articleInfoId =  msgList.get(position).getArticleInfoId();
                    nowCommentId = msgList.get(position).getCommentId();
                    YiJiaHuNetWorkUtils.getSystemMsgKind(articleInfoId,handler);
//                    Intent systemInfo = new Intent(mContext,ArticleDetailActivity.class);
//                    systemInfo.putExtra("articleId",articleInfoId);
//                    startActivity(systemInfo);


                    //finish();
                }
            }
        });
        refreshLayout.setOnRefreshListener(this);

        ll_empty = (LinearLayout) findViewById(R.id.ll_department_empty);
        tv_empty = (TextView) findViewById(R.id.empty_text);
        ll_empty.setVisibility(View.GONE);
        tv_empty.setText(R.string.nosysteminfo_text_title);
    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.getSystemMsg(pageIndex, pageSize, handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back://back
                finish();
                break;

        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        loading();
        loadmoreType = 1;
        pageIndex = 1;
        YiJiaHuNetWorkUtils.getSystemMsg(pageIndex, pageSize, handler);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        if (totalCount <= msgList.size()) {
            toastMsg(R.string.nomore);
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        } else {
            loading();
            loadmoreType = 2;
            pageIndex++;
            YiJiaHuNetWorkUtils.getSystemMsg(pageIndex, pageSize, handler);
        }
    }
}